package com.example.neusoft.startproject_wk.model;

import com.example.neusoft.startproject_wk.Iface.WeatherIface;
import com.example.neusoft.startproject_wk.Iface.WeatherListener;
import com.example.neusoft.startproject_wk.bean.WeatherResult;
import com.example.neusoft.startproject_wk.service.Weather51Service;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Neusoft on 2017/3/10.
 */

public class Weather51Model implements WeatherIface {
   private Retrofit retrofit;
    private  String BASEURL51="http://weather.51wnl.com/weatherinfo/";
    public Weather51Model() {
        //3-1
        retrofit=new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())
                .baseUrl(BASEURL51).build();
    }

    @Override
    public void getWeather(String cityNumber, final WeatherListener listener) {
        //3-2
        Weather51Service weather51Service=retrofit.create(Weather51Service.class);
        Call<WeatherResult>  call=weather51Service.getResult(cityNumber,1);
        //3-3
        call.enqueue(new Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                if(response.isSuccessful() && response.body()!=null)
                    listener.onResponse(response.body().getWeatherInfo());
                else listener.onFail("onresponse error");
            }

            @Override
            public void onFailure(Call<WeatherResult> call, Throwable throwable) {
                listener.onFail(throwable.toString());
            }
        });
    }
}
